package service

import (
	"errors"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/admin/service/dto"
	"tuowei.com/common/actions"
	"tuowei.com/pkg/xlog"
)

type SysCourseWare struct {
	service.Service
}

func (e SysCourseWare) Insert(req *dto.SysCourseWareInsertReq) (int, error) {
	var err error
	var data models.SysCourseWare
	//var i int64
	//err = e.Orm.Model(&data).Where("name = ?", req.Name).Count(&i).Error
	//if err != nil {
	//	e.Log.Errorf("db error: %s", err)
	//	return 0, err
	//}
	//if i > 0 {
	//	err := errors.New("子章节已存在！")
	//	e.Log.Errorf("db error: %s", err)
	//	return 0, err
	//}

	req.Generate(&data)

	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return 0, err
	}

	return data.Id, nil
}

func (e SysCourseWare) UpdateCourseWare(req *dto.SysCourseWareChangeReq) error {
	var err error
	var model = models.SysCourseWare{}
	e.Orm.First(&model, req.GetId())
	req.Generate(&model)
	db := e.Orm.Save(&model)
	err = db.Error
	if err != nil {
		e.Log.Errorf("Service UpdateStatus error:%s", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")

	}
	return nil
}

func (e SysCourseWare) Update(req *dto.SysCourseWareUpdateReq) error {
	var err error
	var model = models.SysCourseWare{}
	//var i int64
	//err = e.Orm.Model(&model).Where("name = ?", req.Name).Where("id <> ?", req.Id).Count(&i).Error
	//if err != nil {
	//	e.Log.Errorf("db error: %s", err)
	//	return err
	//}
	//if i > 0 {
	//	err = errors.New("子章节已存在！")
	//	e.Log.Errorf("db error: %s", err)
	//	return err
	//}
	e.Orm.First(&model, req.GetId())
	req.Generate(&model)
	db := e.Orm.Save(&model)
	err = db.Error
	if err != nil {
		e.Log.Errorf("Service UpdateStatus error:%s", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")

	}
	return nil
}

func (e SysCourseWare) DeleteCourseWare(req *dto.SysCourseWareDeleteByIdReq, p *actions.DataPermission) error {
	var err error
	var data models.SysCourseDelete
	var questionBankLinkModel models.SysQuestionBankLinkDelete

	e.Orm.Transaction(func(tx *gorm.DB) error {
		db := tx.Table(data.TableName()).Where("id in (?)", req.GetId()).Delete(data)
		if err = db.Error; err != nil {
			e.Log.Errorf("Error found in  RemoveSysCourseWare : %s", err)
			return err
		}
		if db.RowsAffected == 0 {
			return errors.New("无权删除该数据")
		}
		err = tx.Debug().
			Where("link_type = ?", 1).
			Where("chapter_id in (?)", req.GetId()).
			Where("link_id = ?", req.LinkId).Delete(&questionBankLinkModel).Error
		if err != nil {
			return err
		}
		return nil
	})

	return nil
}

func (e SysCourseWare) DeleteLargeChapter(req *dto.SysCourseWareDeleteLargeChapterByIdReq, p *actions.DataPermission) error {
	var err error
	var data models.SysCourseWare

	return e.Orm.Transaction(func(tx *gorm.DB) error {

		err = tx.Model(&data).
			Scopes(
				actions.Permission(data.TableName(), p),
			).Delete(&data, req.GetId()).Error
		if err != nil {
			return err
		}

		err = tx.Model(&data).
			Scopes(
				actions.Permission(data.TableName(), p),
			).Where("pid = ?", req.GetId()).Delete(&data).Error
		if err != nil {
			return err
		}
		return nil
	})

	return nil
}

// Get 获取SysCourse对象
func (e *SysCourseWare) Get(d *dto.SysCourseWareById, p *actions.DataPermission, model *models.SysCourseWare) error {
	var data models.SysCourseWare

	err := e.Orm.Model(&data).Debug().
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error

	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		xlog.Errorf("db error: %s", err)
		return err
	}
	if err != nil {
		xlog.Errorf("db error: %s", err)
		return err
	}
	if err != nil {
		xlog.Errorf("db error: %s", err)
		return err
	}

	return nil
}
